<?php
session_start();
$act = isset($_GET['act']) ? $_GET['act'] : (isset($_POST['act']) ? $_POST['act'] : '');
if($act == '') {
    return json(['success' => false, 'error' => '请求失败，没有找到服务！']);
}
if($act == 'checkSms') {
    $mobile  = isset($_POST['mobile']) ? $_POST['mobile'] : '';
    $captcha = isset($_POST['captcha']) ? $_POST['captcha'] : '';
    if($mobile == '') {
        return json(['success' => false, 'error' => '获取短信验证码失败，手机号码不能为空！']);
    }
    if($captcha == '') {
        return json(['success' => false, 'error' => '获取短信验证码失败，验证码不能为空！']);
    }

    if($captcha != $_SESSION['captcha']) {
        return json(['success' => false, 'error' => '获取短信验证码失败，验证码不正确！']);
    }
    $gateway = 'http://sms.chanzor.com:8001/sms.aspx?action=send&account=meizanchen&password=154214&mobile=%s&content=%s&sendTime=';
    $code    = mt_rand(1234, 9999);
    $sms     = '感谢关注美赞臣A+妈妈会，请输入验证码' . $code . '完成手机号码验证。本短信免费回复TD退订【美赞臣】';

    $_SESSION[$mobile] = $code;

    $gateway = sprintf($gateway, $mobile, rawurlencode($sms));

    $retMsg = request($gateway, 'GET');
    if($retMsg != '') {
        $ret = json_decode(json_encode(simplexml_load_string($retMsg)), true);
        if(is_array($ret) && isset($ret['returnstatus'])) {
            if(strtolower($ret['returnstatus']) == 'success') {
                return json(['success' => true, 'msg' => '短信已经提交成功，请耐心等待！']);
            } else {
                return json(['success' => false, 'error' => '短信发送失败，系统错误，请稍候再试！']);
            }
        } else {
            return json(['success' => false, 'error' => '短信发送失败，系统错误，请稍候再试！']);
        }
    }
}
if($act == 'register') {
    $mobile   = isset($_POST['mobile']) ? $_POST['mobile'] : '';
    $mcode    = isset($_POST['mcode']) ? $_POST['mcode'] : '';
    $name     = isset($_POST['name']) ? $_POST['name'] : '';
    $birthday = isset($_POST['birthday']) ? $_POST['birthday'] : '';
    $code     = isset($_POST['code']) ? $_POST['code'] : '';
    $question = isset($_POST['question']) ? $_POST['question'] : '';
    $brand    = isset($_POST['brand']) ? $_POST['brand'] : '';
    if($mobile == '') {
        return json(['success' => false, 'error' => '提交失败，手机号码不能为空！']);
    }
    if($mcode != $_SESSION[$mobile]) {
        return json(['success' => false, 'error' => '提交失败，手机验证码不正确！']);
    }
    if($mobile == '') {
        return json(['success' => false, 'error' => '提交失败，手机号码不能为空！']);
    }
    if($name == '') {
        return json(['success' => false, 'error' => '提交失败，姓名不能为空！']);
    }
    if($birthday == '') {
        return json(['success' => false, 'error' => '提交失败，预产期/宝宝生日不能为空！']);
    }
    if($code == '') {
        return json(['success' => false, 'error' => '提交失败，指定代码不能为空！']);
    }
    if($question == '') {
        return json(['success' => false, 'error' => '提交失败，宝宝奶粉关注的问题不能为空！']);
    }
    if($brand == '') {
        return json(['success' => false, 'error' => '提交失败，正在给宝宝使用的品牌不能为空！']);
    }
    return json(['success' => true, 'msg' => '提交成功，感谢您的参与！']);
}
function json(array $data, $unescaped = true) {
    //var_dump($data);
    header("Expires: 0");
    header("Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0");
    header("Pragma: no-cache");
    header('Content-type: application/json;charset=utf-8');
    if($unescaped) {
        if(version_compare(PHP_VERSION, '5.4.0') >= 0) {
            echo json_encode($data, JSON_UNESCAPED_UNICODE);
        } else {
            $json = json_encode($data);
            echo preg_replace_callback("#\\\\u([0-9a-f]{4})#", create_function('$matches', 'return iconv("UCS-2BE","UTF-8",pack("H4", $matches[1]));'), $json);
        }
    } else {
        echo json_encode($data);
    }
    return;
}

function request($url, $method = 'GET', $params = [], $timeout = 30, $extHeaders = []) {
    if(!function_exists('curl_init')) {
        exit('Need to open the curl extension');
    }
    $method = strtoupper($method);
    $ci     = curl_init();
    curl_setopt($ci, CURLOPT_USERAGENT, '59im.Cn MOBILE');
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 20);
    curl_setopt($ci, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ci, CURLOPT_HEADER, false);
    $headers = (array)$extHeaders;
    switch($method) {
        case 'POST':
            curl_setopt($ci, CURLOPT_POST, true);
            if(!empty($params)) {
                curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
            }
            break;
        case 'DELETE':
        case 'GET':
            $method == 'DELETE' && curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
            if(!empty($params)) {
                $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
            }
            break;
    }
    curl_setopt($ci, CURLINFO_HEADER_OUT, true);
    curl_setopt($ci, CURLOPT_URL, $url);
    if($headers) {
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
    }
    $response = curl_exec($ci);
    curl_close($ci);
    return $response;
}